package com.operate.api.operate.meal.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.pagehelper.PageInfo;
import com.operate.api.base.*;
import com.operate.api.operate.meal.domain.vo.*;
import com.operate.api.operate.meal.service.IPackageService;
import com.operate.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api( tags = ApiIndex.PackageController)
@RequestMapping(value = "/api/operate/package")
@RestController
@SaCheckLogin
public class PackageController {

    @Autowired
    private IPackageService service;

    @ApiOperation(value = "查询套餐列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<PackageVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute PackageSearchVO searchVO) {
        List<PackageVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建套餐")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody PackageAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除套餐")
    @PostMapping(value = "/delete")
    public R delete( @ApiParam(value = "套餐ID") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }


    @ApiOperation(value = "禁言/启用-套餐")
    @PostMapping(value = "/disablePackage")
    public R disablePackage(@ApiParam(value = "参数")  @RequestBody PackageStateVO vo) {
        service.disablePackage(vo);
        return new R();
    }


    @ApiOperation(value = "修改套餐")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody PackageUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }




}
